package cc.mrbird.febs.system.controller;

import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.job.entity.Job;
import cc.mrbird.febs.system.entity.DataDictionary;
import cc.mrbird.febs.system.service.IDataDictionaryService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

@Slf4j
@Validated
@RestController
@RequestMapping("dataDictionary")
public class DataDictionaryController extends BaseController {
    @Autowired
    private IDataDictionaryService dataDictionaryService;

    @GetMapping
    @RequiresPermissions("dataDictionary:view")
    public FebsResponse dataDictionaryList(QueryRequest request, DataDictionary dataDictionary) {
        Map<String, Object> dataTable = getDataTable(this.dataDictionaryService.findDataDictionarys(request, dataDictionary));
        return new FebsResponse().success().data(dataTable);
    }

    @PostMapping
    @RequiresPermissions("dataDictionary:add")
    public FebsResponse addDataDictionary(@Valid DataDictionary dataDictionary) {
        this.dataDictionaryService.createDataDictionary(dataDictionary);
        return new FebsResponse().success();
    }

    @GetMapping("delete/{dataDictionaryIds}")
    @RequiresPermissions("dataDictionary:delete")
    public FebsResponse deletedataDictionary(@NotBlank(message = "{required}") @PathVariable String dataDictionaryIds) {
        String[] ids = dataDictionaryIds.split(StringPool.COMMA);
        this.dataDictionaryService.deleteDataDictionarys(ids);
        return new FebsResponse().success();
    }

    @PostMapping("addContents/{dataDictionaryContents}")
    @RequiresPermissions("dataDictionary:add")
    public FebsResponse addDataDictionarys(@PathVariable String dataDictionaryContents) {
        String[] contents = dataDictionaryContents.split(StringPool.COMMA);
        this.dataDictionaryService.addDataDictionaryContents(contents);
        return new FebsResponse().success();
    }

    @PostMapping("update")
    public FebsResponse updateDataDictionary(@Valid DataDictionary dataDictionary) {
        this.dataDictionaryService.updateDataDictionary(dataDictionary);
        return new FebsResponse().success();
    }

    @GetMapping("excel")
    @RequiresPermissions("dataDictionary:export")
    @ControllerEndpoint(exceptionMessage = "导出Excel失败")
    public void export(QueryRequest request, DataDictionary dataDictionary, HttpServletResponse response) {
        List<DataDictionary> dataDictionarys = this.dataDictionaryService.findDataDictionarys(request, dataDictionary).getRecords();
        ExcelKit.$Export(DataDictionary.class, response).downXlsx(dataDictionarys, false);
    }
}
